/**
 * Created by Administrator on 2016/6/16.
 */

var doctorInfo ='<section class="commentDetails" v-for="item in items"><p class="zhezhao"></p>' +
    '<section v-if="item.score == \'1\'"><img src="../images/HalfGray.png"><img src="../images/gray.png"><img src="../images/gray.png"><img src="../images/gray.png"><img src="../images/gray.png"> </section>' +
    '<section v-if="item.score == \'2\'"> <img src="../images/star.png"><img src="../images/gray.png"><img src="../images/gray.png"> <img src="../images/gray.png"><img src="../images/gray.png"></section>' +
    '<section v-if="item.score == \'3\'"><img src="../images/star.png"><img src="../images/HalfGray.png"><img src="../images/gray.png"><img src="../images/gray.png"><img src="../images/gray.png"></section>' +
    '<section v-if="item.score == \'4\'"><img src="../images/star.png"><img src="../images/star.png"><img src="../images/gray.png"><img src="../images/gray.png"> <img style="margin-left: 0%;" src="../images/gray.png"></section>' +
    '<section v-if="item.score == \'5\'"><img src="../images/star.png"><img src="../images/star.png"><img src="../images/HalfGray.png"><img src="../images/gray.png"><img src="../images/gray.png"></section>' +
    '<section v-if="item.score == \'6\'"><img src="../images/star.png"> <img style="margin-left: 0%;" src="../images/star.png"> <img  style="margin-left: 0%;" src="../images/star.png"> <img style="margin-left: 0%;" src="../images/gray.png"> <img style="margin-left: 0%;" src="../images/gray.png"> </section>' +
    '<section v-if="item.score == \'7\'"><img src="../images/star.png"><img src="../images/star.png"> <img style="margin-left: 0%;" src="../images/star.png"> <img style="margin-left: 0%;"  src="../images/HalfGray.png"> <img style="margin-left:0" src="../images/gray.png"> </section> ' +
    '<section v-if="item.score == \'8\'"> <img src="../images/star.png"> <img style="margin-left: 0%;" src="../images/star.png"><img src="../images/star.png"> <img style="margin-left: 0%;" src="../images/star.png"> <img style="margin-left: 0%;" src="../images/gray.png"> </section>' +
    ' <section v-if="item.score == \'9\'"> <img src="../images/star.png"><img src="../images/star.png"><img src="../images/star.png"><img src="../images/star.png"><img src="../images/HalfGray.png"></section>' +
    '<section v-if="item.score == \'10\'"> <img style="margin-left: 0%;" src="../images/star.png"> <img style="margin-left: 0%;" src="../images/star.png"> <img style="margin-left: 0%;" src="../images/star.png"> <img style="margin-left: 0%;" src="../images/star.png"  > <img src="../images/star.png"  style="margin-left: 0%;"> </section>' +
    '<section><span>{{item.score}}分</span></section><section class="like_num"><span >{{item.zan}}</span><img v-if="item.if_zan == \'0\'" src="../images/like.png" class="like"><img v-if="item.if_zan == \'1\'" src="../images/alreadyLike.png" class="like"></section><p>{{item.content}}</p><section><img v-if="item.nic_thumb == \'\'" src="../images/userDefault.png"> <img v-if="item.nic_thumb != \'\'" v-bind:src="item.nic_thumb"><section><p>{{item.nic_name}}</p><p>{{item.time_str}}</p> </section><p v-if="item.type == \'1\'">电话咨询</p> <p v-if="item.type == \'2\'">约见</p></section><section class="extra"><p class="c_id">{{item.c_id}}</p></section></section>';
//

var page  = 1;
//自定义vue模板
var loading = false;
//分页
$(document).ready(function(){
    $(window).scroll(function(){
        if (loading)
        {
            return;
        }
        if  ($(window).scrollTop() > $(document).height() - $(window).height() - 200)
        {
            loading = true;
            setTimeout(function(){
                nextPage() ;
            }, 250);
        }
    });
});
//doctorInfo动画
$(function(){
    $("#back").click(function(){
        history.go(-1);
    })
    //整体滑动效果
    var mainPart = document.getElementById("mainPart");
        $("#mainPart").scroll(function(){
            if($("#mainPart").scrollTop()>450){
                $("header").fadeIn(600);
                $("#menu").fadeIn(600);
            }else{
                $("header").fadeOut(600);
                $("#menu").fadeOut(600);
            }
        }).trigger('scroll');

});


//切换信息
$(document).ready(function(){
    $("section.menu a").click(function(){
        $("section.menu a").removeClass("active");
        var i = $(this).index();
        $("#menu a").eq(i).addClass("active");
    })
    $("section.menu a:first-child").click(function(){
        $(".doctorIntro1").css("display","block");
        $(".comment").css("display","none");
        $(".hairDairy").css("display","none");
    })
    $("section.menu a:nth-child(2)").click(function(){
        $(".doctorIntro1").css("display","none");
        $(".comment").css("display","block");
        $(".hairDairy").css("display","none");
    })
    $("section.menu a:nth-child(3)").click(function(){
        $(".doctorIntro1").css("display","none");
        $(".comment").css("display","none");
        $(".hairDairy").css("display","block");
    })
    $("#close").click(function(){
        $(".total").css("display","none");
        $(".mask").css("display","none");
        $(".son1").css("display","none");
    })
    $(".son1 p:nth-child(2)").click(function(){
        createWindow("暂无更多数据");
    })
    $(".mask").click(function(){
        $(".total").css("display","none");
        $(".mask").css("display","none");
        $(".son1").css("display","none");
    })
});

//获取数据
$(document).ready(function(){
    var str = window.location.search;
    var d_id1 = getQueryString("id");
    var d_id;
    if(d_id1){
        d_id =d_id1;
    }else{
        d_id=sessionStorage.d_id;
    }
    //强制ajax同步
    $.ajaxSettings.async=false;
    commentList();
    postArray.d_id =d_id;
    postArray.u_id =u_id;
    $.post(commonval+"/getDoctorInfo",postArray,function(data){
        if(data.status==1){
            sessionStorage.doctorimg = data.data.nic_thumb;
            sessionStorage.doctorname = data.data.name;
            sessionStorage.doctorzhiwu = data.data.zhiwu;
            sessionStorage.hospital_name = data.data.hospital_name;
            $("section.menu a").click(function(){
                $("section.menu a").removeClass("active");
                var i = $(this).index();
                $("#menu a").eq(i).addClass("active");
                $("#doctorInfo .menu a").eq(i).addClass("active");
            })
            //header 文字更改
            $("title").text(data.data.name+"医生的主页");
            $("header p").text(data.data.name);
            $("#doctorInfo").css("display","block");
            $(".button button").removeAttr("disabled");
            $(".button").css("display","block");
            if(data.data.is_consult == 1){
                $(".button section:nth-child(3)").click(function(){
                    if(u_id){
                        var url ="submitOrder.html";
                        sessionStorage.type = "1";
                        location.href = url+"?type="+sessionStorage.type;
                    }
                })
            }else{
                $(".button section:nth-child(3) p").text("医生繁忙，暂不提供咨询");
                $(".button section:nth-child(3)").addClass("unclick");
            }
            var docListMember = new Vue({
                el: '#doctorInfo1',
                data: data.data
            });
            var docListMember = new Vue({
                el: '#doctorInfo',
                data: data.data
            });
            $("#menu a:nth-child(2)").text($("#doctorInfo .menu a:nth-child(2)").text());
            $("#menu a:nth-child(3)").text($("#doctorInfo .menu a:nth-child(3)").text());
            $("#doctorInfo1").css("display","block");
            sessionStorage.url = data.data.share_url;
            //百度分享代码
            window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdStyle":"0","bdSize":"24",bdUrl:sessionStorage.url},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
            //"bdUrl":sessionStorage.url

            if(data.data.if_follow == 0){
                $(".button section:nth-child(2) img").attr("src","../images/doctor_noticeW.png")
            }else{
                $(".button section:nth-child(2) img").attr("src","../images/doctor_notice_preB.png");
            }
            var i = 1;
            $(".button section:nth-child(2)").click(function(e){
                if(u_id){
                    i++;
                    if(data.data.if_follow == 0){
                        if(i%2 ==1){
                            $(".button section:nth-child(2) img").attr("src","../images/doctor_noticeW.png");
                            createWindow("取消关注成功");
                        }else{
                            $(".button section:nth-child(2) img").attr("src","../images/doctor_notice_preB.png");
                            createWindow("关注成功");
                        }
                    }else{
                        if(i%2 ==0){
                            $("header img:last-child").attr("src","../images/doctor_noticeW.png");
                            createWindow("取消关注成功");
                        }else{
                            $("header img:last-child").attr("src","../images/doctor_notice_preB.png");
                            createWindow("关注成功");
                        }
                    }
                    $.post(commonval+"/follow_doctor",{
                        u_id:u_id,
                        d_id:d_id,
                        device:"wap",
                    },function(data){

                    });
                }

                e.stopPropagation();
            });
            $(".optionGroup section:nth-child(2) p:first-child span:nth-child(2)").text("￥"+data.data.tel_price+"/分钟");
            $(".optionGroup section:nth-child(2) p:nth-child(2) span:nth-child(2)").text("已有"+data.data.response_times+"人咨询");
            sessionStorage.d_name = data.data.name;
            sessionStorage.tel_price = data.data.tel_price;
            //立即咨询菜单
            $(".mask").click(function(){
                $(".mask").css("display","none");
                $(".optionGroup").css("display","none");
            })
            $(".optionGroup").click(function(){
                $(".mask").toggle();
                $(".optionGroup").toggle();
            })
        }else{
            $(".noInform").css("display","block");
            $("#doctorInfo").css("display","none");
        }
    })
});

//评论接口
function commentList(){
    var str = window.location.search;
    var d_id1 = getQueryString("id");
    var d_id;
    if(d_id1){
        d_id =d_id1;
    }else{
        d_id=sessionStorage.d_id;
    }
    $.post(commonval+"/comment_list",{
        u_id:u_id,
        d_id:d_id,
        device:"wap",
        page:''
    },function(data){
        if(data.status == 1){
            $("#doctorInfo2").append(doctorInfo);
            var consultList = new Vue({
                el: '#doctorInfo2',
                data: {
                    items: data.data
                }
            });
        //$(".commentDetails section:nth-child(2) img:not(:first-child)").css("margin-left","4%");
        }else{
            $("#doctorInfo2").css("display","none")
        }

    });
}

//第一条日记逻辑
$(document).ready(function(){
    var str = window.location.search;
    var d_id1 = getQueryString("id");
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
    var d_id;
    if(d_id1){
        d_id =d_id1;
    }else{
        d_id=sessionStorage.d_id;
    }

    $.post(commonval+"/doctor_book_first",{
        u_id:u_id,
        d_id:d_id,
        user_token:user_token,
        device:"wap",
        page:"",
        session_id:session_id
    },function(data){
        var consultList = new Vue({
            el: '#doctorInfo3',
            data: {
                items: data.data
            }
        });
        var dataUse = data.data;
        //<li><a href="../images/content.png"><img src="../images/content.png" alt="Image 001" /></a></li>
        for(var i=0;i<dataUse.length;i++){
            var content_image = dataUse[i].content_image;
            //$(".imgGroup").attr("id","Gallery"+i);
            for(var j=0;j<content_image.length;j++){
                var href = content_image[j];
                var img = $("<img>" );
                var z = j+1;
                var alt = z+"/"+content_image.length;
                img.attr("src",href);
                img.attr("alt",alt);
                var a = $("<a href='#'></a>");
                a.attr("href",href);
                var li = $("<li></li>");
                img.appendTo(a);
                a.appendTo(li);
                li.appendTo($(" .hairDairyDetails").eq(i).children(".imgGroup"));
                $(" .hairDairyDetails").eq(i).children(".imgGroup").parent().attr("id","Gallery"+i);
            }
        }
    });
});
//获取全部日记
$(document).ready(function(){
    (function(window, $, PhotoSwipe){
        var fullDairy = '<img src="../images/head@2x.png"><p>没吃药先生生发日记</p><section class="father dairy" ><section class="time" v-for="item in items" v-if="item.style==1"><img src="../images/crcle2@2x.png" class="first" ><p>{{item.create_time}}</p><img src="../images/icon@2x.png"/><p>{{item.more_agree}}</p><section class="son"><p>{{item.content}}</p></section></section><section class="time" v-for="item in items" v-if="item.style==0"><img src="../images/cicle@2x.png" /><p>{{item.create_time}}</p><img src="../images/icon@2x.png"/><p>{{item.more_agree}}</p><section class="son"><p>{{item.content}}</p></section></section><section class="time" v-for="item in items" v-if="item.style==2"> <img src="../images/cicle@2x.png" /><p>{{item.create_time}}</p><img src="../images/icon@2x.png"/><p>{{item.more_agree}}</p><section class="son"><p>{{item.content}}</p> </section> <img src="../images/crcle2@2x.png" class="over"/></section></section>';
        $(".more").click(function() {
            $(".total").css("display", "block");
            $(".son1").css("display", "block");
            $(".mask").css("display", "block");
            var order_no = $(this).parents(".hairDairyDetails").children(".extra").children("p.order_num").text();
            $.post(commonval+"/doctor_note_list", {
                order_num: order_no,
                u_id: u_id,
                user_token: user_token,
                device: "wap",
                session_id:session_id
            }, function (data) {
                $("#total").empty();
                $("#total").append(fullDairy);
                var consultList = new Vue({
                    el: '#total',
                    data: {
                        items: data.data
                    }
                });
                var nic_thumb = data.data[0].nic_thumb;
                var user_name = data.data[0].user_name;
                $(".total>img:first-child").attr("src", nic_thumb);
                $(".total>p:nth-child(2)").text(user_name + "的生发日记");
                //日记列表渲染
                var dataUse = data.data;
                //<li><a href="../images/content.png"><img src="../images/content.png" alt="Image 001" /></a></li>
                for (var i = 0; i < dataUse.length; i++) {
                    var content_image = dataUse[i].content_image;
                    //$(".imgGroup").attr("id","Gallery"+i);
                    for (var j = 0; j < content_image.length; j++) {
                        var href = content_image[j];
                        var img = $("<img>");
                        var z = j + 1;
                        var alt = z + "/" + content_image.length;
                        img.attr("src", href);
                        img.attr("alt", alt);
                        var a = $("<a href='#'></a>");
                        a.attr("href", href);
                        var li = $("<li></li>");
                        img.appendTo(a);
                        a.appendTo(li);
                        li.appendTo($(".time").eq(i).children(".son"));
                        $(".time").eq(i).children(".son").parent().attr("id", "diary" + i);
                    }
                }
                var options = {};
                $('#diary0 a').photoSwipe(options);
                $('#diary1 a').photoSwipe(options);
                $('#diary2 a').photoSwipe(options);
                $('#diary3 a').photoSwipe(options);
                $('#diary4 a').photoSwipe(options);
                $('#diary5 a').photoSwipe(options);
                $('#diary6 a').photoSwipe(options);
                $('#diary7 a').photoSwipe(options);
                $('#diary8 a').photoSwipe(options);
                $('#diary9 a').photoSwipe(options);
            })
        });
    }(window, window.jQuery, window.Code.PhotoSwipe));

});

//评论点赞逻辑
$(document).ready(function(){
    $(".commentDetails .like").click(function(){
        var str = window.location.search;
        var d_id1 = getQueryString("id");
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
        var d_id;
        if(d_id1){
            d_id =d_id1;
        }else{
            d_id=sessionStorage.d_id;
        }
        var c_id = $(this).parent().parent().children(".extra").children("p").text();
        var val = $(this).parent().parent().children(".like_num").children("span").text();
        var span = $(this).parent().parent().children(".like_num").children("span");
        var img = $(this).parent().parent().children(".like_num").children("img")
        sessionStorage.likeVal = parseInt(val)+1;
        $.post(commonval+"/comment_zan",{
            u_id:u_id,
            d_id:d_id,
            user_token:user_token,
            device:"wap",
            c_id:c_id,
            session_id:session_id
        },function(data){
            if(data.status == 1){
                if(data.data.type==1){
                    span.text(sessionStorage.likeVal);
                    img.attr("src","../images/alreadyLike.png");
                }
                createWindow(data.data.msg);
            }else{
                createWindow(data.data.msg);
            }
        });
    })
});
//日记点赞逻辑
$(document).ready(function(){
    $(".hairDairyDetails .like").click(function(){
        var str = window.location.search;
        var d_id1 = getQueryString("id");
        var d_id;
        if(d_id1){
            d_id =d_id1;
        }else{
            d_id=sessionStorage.d_id;
        }

        var note_id = $(this).parent().parent().parent().children(".extra").children("p").text();
        var val = $(this).parent().parent().parent().children("section:first-child").children(".like_num").children("span").text();
        var span = $(this).parent().parent().parent().children("section:first-child").children(".like_num").children("span");
        var img = $(this).parent().parent().parent().children("section:first-child").children(".like_num").children("img")
        sessionStorage.likeVal1 = parseInt(val)+1;
        $.post(commonval+"/note_update",{
            u_id:u_id,
            d_id:d_id,
            user_token:user_token,
            device:"wap",
            note_id:note_id,
            session_id:session_id
        },function(data){
            if(data.status == 1){
                if(data.data==1){
                    span.text(sessionStorage.likeVal1);
                    img.attr("src","../images/alreadyLike.png");
                }
                createWindow(data.msg);
            }else{
                createWindow(data.msg);
            }
        });
    })
});
//星星逻辑

$(document).ready(function(){
    $(".commentDetails .rating input").attr("disabled","true");
});

//使按钮固定在底部
$(document).ready(function(){
    var $width = $(window).width();
    var $height = $(window).height();
    //$(".button").css("top",$height-160);
    $(".optionGroup").css("top",$height-340);
    $(".noConsult").css("top",$height-141);
    $(".mask").css({"width":$width,"height":$height});
});

//切换信息
$(document).ready(function(){
    $("section.menu span").click(function(){
        $("section.menu span").removeClass("active");
        $(this).addClass("active");
    })
    $("section.menu span:first-child").click(function(){
        $(".doctorIntro1").css("display","block");
        $(".comment").css("display","none");
        $(".hairDairy").css("display","none");
        $(".basicInfo").css("display","block");
    })
    $("section.menu span:nth-child(2)").click(function(){
        $(".doctorIntro1").css("display","none");
        $(".comment").css("display","block");
        $(".hairDairy").css("display","none");
        $(".basicInfo").css("display","none");
    })
    $("section.menu span:nth-child(3)").click(function(){
        $(".doctorIntro1").css("display","none");
        $(".comment").css("display","none");
        $(".hairDairy").css("display","block");
        $(".basicInfo").css("display","none");
    })
});
nextPage = function(){
    page++;
    var d_id = sessionStorage.d_id;
    $.ajaxSettings.async=false;
    $.post(commonval+"/comment_list",{
        u_id:u_id,
        d_id:d_id,
        user_token:user_token,
        device:"wap",
        session_id:session_id,
        page:page
    },function(data){
        if(data.status == "1"&& data != null ){
            $("#doctorInfo2").append(doctorInfo);
            var consultList = new Vue({
                el: '#doctorInfo2',
                data: {
                    items: data.data
                }
            });
        }else{
            page--;
        }
    });
}

